package src.edu.wayne.cs.csc4110.hw6.plugins;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import src.edu.wayne.cs.csc4110.hw6.interfaces.PluginToFrameWorkInterface;

public class ExamplePluginTwo extends ExamplePluginOne {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6202657604942030333L;
	int Min = 0;
	int Max = 100;
	Integer answer = Min + (int)(Math.random() * ((Max - Min) + 1));;
	Integer guess = 1;
	public String gameText = "";

	public Integer input = 1;
	//JLabel label1 = new JLabel("Welcome to the number guessing game!");
	public JLabel label2 = new JLabel("");
	public JTextField newText = new JTextField();
	JButton guessButton;
	JPanel myPanel;
	
	
	public ExamplePluginTwo(PluginToFrameWorkInterface frameWork) {
		super(frameWork);
		myPanel = new JPanel();
		JPanel backFrame = new JPanel();
		
		backFrame.setLayout(new BorderLayout(0, 150));		
		//backFrame.add(label1, BorderLayout.NORTH);
		backFrame.add(label2, BorderLayout.NORTH);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,0));
		
		buttonPanel.add(getGuessButton());
		buttonPanel.add(getQuitButton());
		backFrame.add(buttonPanel, BorderLayout.SOUTH);
		backFrame.add(newText, BorderLayout.CENTER);
		myPanel.add(backFrame);
		restart();
	}

	@Override
	public void startGame() {
		setPlayerList(null);
		setCurrentUser(getNextPlayer());
		String titleString = new String(currentUser.getFullname() + " Guess a number between 1 - 100");
		frameWork.setCurrentPanel(myPanel, titleString);
	}
	
	public void SetGameText( Integer guess ){
		System.out.println("buttonClick");
		CheckAnswer( guess );
		//label1.setText(gameText);

	}
			
	public void CheckAnswer( Integer Guess ){
		if( Guess == answer ){
			label2.setText("Congratulations " + currentUser.getFullname() + " You've Won!");
			restart();
		}
		else if( Guess < answer) {
			label2.setText("Too Low");
			setCurrentUser(getNextPlayer());
		}
		else if( Guess > answer){
			label2.setText("Too High");
			setCurrentUser(getNextPlayer());
		}
		String titleString = new String(currentUser.getFullname() + " Guess a number between 1 - 100");
		frameWork.setCurrentPanel(myPanel, titleString);
	}
			
	public void restart (){

		Min = 0;
		Max = 100;
		answer = Min + (int)(Math.random() * ((Max - Min) + 1));

	}
	


public JButton getGuessButton(){
	guessButton	 = new JButton("Guess");
	guessButton.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent e) 
		{
			
			try{
					input = Integer.parseInt(newText.getText());						
					guess = input;
					SetGameText(guess);
			}
			catch (NumberFormatException e1)
			{
				JOptionPane.showMessageDialog(null, "Enter a number between 1 and 100");	
			}
			
		}
			
	}
	);
	return guessButton;
	
	}

private JButton getQuitButton(){
	JButton jbtnSubmitLogin = new JButton("Quit");
	jbtnSubmitLogin.addActionListener(
			new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					endGame();
				}
			}
		);  
	
	return jbtnSubmitLogin;
}

}
